package com.culture.bootdemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.culture.bootdemo.model.dto.CommonDTO;
import com.culture.bootdemo.po.Answer;
import com.culture.bootdemo.common.basevo.PageUtil;
import com.culture.bootdemo.common.basevo.Result;
import com.culture.bootdemo.dao.AnswerMapper;
import com.culture.bootdemo.model.req.UserReq;
import com.culture.bootdemo.utils.NumberUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;


/**
 * @Author: wenjl
 * @Description: 数据处理controller
 * @Date: 2021/2/4 14:41
 */
@RestController
@Slf4j
@RequestMapping("/data")
public class DataController {

    @Autowired
    private AnswerMapper answerMapper;

    /**
     * 分页查询，自定义
     *
     * @param req
     * @return
     */
    @PostMapping("pageQuery1")
    public Result<Object> pageQuery1(@Valid @RequestBody UserReq req) {
        //先根据查询条件查询总条数
        int total = 1234;

        //分页查询中，手动获取mysql limit语句中的第一个参数 ：
        int offset = PageUtil.getOffset(req.getCurrentPage(), req.getLimit());
        List<Answer> answers = answerMapper.selectByLimit(offset, req.getLimit());

        //封装分页查询返回值：
        PageUtil pageInfo = new PageUtil(req.getLimit(), req.getCurrentPage(), total);
        System.out.println(pageInfo);
        return Result.builder().data(answers).page(pageInfo).build();
    }

    /**
     * 分页查询，pageHelper
     *
     * @param req
     * @return
     */
    @PostMapping("pageQuery2")
    public Result<Object> pageQuery2(@Valid @RequestBody UserReq req) {
        //pageHelper 分页查询
        Page<Answer> pages = PageHelper.startPage(req.getCurrentPage(), req.getLimit());
        List<Answer> answers = answerMapper.selectByParams(req);

        //封装分页查询返回值：
        PageUtil pageInfo = new PageUtil(req.getLimit(), req.getCurrentPage(), NumberUtils.toInt(String.valueOf(pages.getTotal())));
        System.out.println(pageInfo);
        return Result.builder().data(answers).page(pageInfo).build();
    }


    /**
     * 分批查询，总量太大，每次查询一定数量的数据，分批查出总量数据
     *
     * @return
     */
    @GetMapping("queryBatch")
    public Result<Object> queryBatch() {
        //数据总量,应先从数据库中查询出总量
        int count = 500_0000;
        //每次查询数量
        int limit = 1000;
        // 遍历循环的次数
        int pages = count / limit + 1;
        for (int i = 0; i < pages; i++) {
            List<Answer> list = answerMapper.selectByLimit(i * limit, limit);
            log.info("第【{}】次数量:{}", i, list.size());
        }
        return Result.builder().data(0).build();
    }

    /**
     * 分批查询 分批查询某个id区间的数(先根据条件查询出所查结果的最大&最小id)
     *
     * @return
     */
    @GetMapping("queryBatch2")
    public Result<Object> queryBatch2() {
        //根据条件查询，每次查询100条，取返回值最大ID，继续查询直到为空
        //select max(id) as maxId, min(id) as minId from t where .......

        Integer minId = 0;
        Integer maxId = 9999999;

        //每批次查询的数量
        Integer limit = 200;

        while (true) {
            List<CommonDTO> list = new ArrayList<>();
            JSONObject param = new JSONObject();
            param.put("minId", minId);
            param.put("limit", limit);
            //list = testMapper.select( "select * from a where id >= #{minId} order by id limit #{limit} ");
            if (CollectionUtils.isEmpty(list)) {
                break;
            }
            //取出当前list中的最大id
            minId = list.get(list.size() - 1).getId();
            if (minId >= maxId) {
                //表示已经取到最大了
                break;
            }
            minId = minId + 1;
        }
        return Result.builder().data(0).build();
    }


    public static void main(String[] args) {
        List<CommonDTO> list = new ArrayList<>();
        list.add(new CommonDTO(1, "1"));
        list.add(new CommonDTO(2, "2"));
        list.add(new CommonDTO(3, "3"));
        list.add(new CommonDTO(4, "4"));
        if (CollectionUtils.isNotEmpty(list)) {
            Integer id = list.get(list.size() - 1).getId();
            System.out.println(id);
        }

    }
}
